package com.learn.algorithm_783;

import com.dataconsruct.TreeNode;
import com.dataconsruct.TreeOperation;
import org.junit.Test;

/**
 * 前中后序，遍历二叉树
 */
public class SearchTreeNode {

    @Test
    public void test() {
        TreeNode root = new TreeNode(4, new TreeNode(3, new TreeNode(1), new TreeNode(2)),
                new TreeNode(5, new TreeNode(6), new TreeNode(7)));
        TreeOperation.show(root);
        System.out.println("--------------  分割线  ----------------");
//        midSearch(root);
//        beforeSearch(root);
        afterSearch(root);
    }

    /**
     * 前序遍历二叉树（根左右），
     */
    public void beforeSearch(TreeNode root) {
        if (root != null) {
            System.out.println("前序遍历：" + root.val);
            beforeSearch(root.left);
            beforeSearch(root.right);
        }
    }

    /**
     * 中序遍历二叉树（左根右），
     */
    public void midSearch(TreeNode root) {
        if (root != null) {
            midSearch(root.left);
            System.out.println("中序遍历：" + root.val);
            midSearch(root.right);
        }
    }

    /**
     * 后序遍历二叉树（根左右），
     */
    public void afterSearch(TreeNode root) {
        if (root != null) {
            afterSearch(root.left);
            afterSearch(root.right);
            System.out.println("后序遍历：" + root.val);
        }
    }

}
